package utils

import (
	"Last/model"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"log"
)

/*
	数据库相关配置文件
   @author：zjy
   @Emial:734219310@qq.com
*/

//打开连接
func OpenConn() *gorm.DB {
	//读取配置
	config := model.ReadConfig()
	//数据库连接字符串
	connStr := config.MySqlUserName + ":" + config.MySqlPassword + "@/" + config.MySqlDbName + "?charset=utf8&parseTime=True&loc=Local"
	//连接数据库
	db, err := gorm.Open("mysql", connStr)
	if err != nil {
		log.Fatal("数据库连接失败：", err)
	}
	return db
}

//关闭连接
func CloseConn(db *gorm.DB) {
	err := db.Close()
	if err != nil {
		log.Fatal("数据库关闭失败")
	}
}
